package com.mycinema.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.mycinema.biz.UserBiz;
import com.mycinema.entity.User;

@Controller
public class UserController {
	@Autowired
	private UserBiz userBiz;

	@RequestMapping("/login")
	public String login() {
		return "login";
	}

	@RequestMapping(value = "/doLogin", method = RequestMethod.POST)
	public String doLogin(String username, String password, Model model,
			HttpSession session) {

		User user = userBiz.checkLogin(username, password);
		if (user != null) {// 登陆成功
			session.setAttribute("user", user);
			return "redirect:index";// 重定向首页
		} else {

//			model.addAttribute("error", "用户名或者密码错误");
//			return "login";
			throw new RuntimeException("用户不存在");
		}

	}

	//局部异常处理的方法
	//@ExceptionHandler(value={RuntimeException.class})
	public String handlerException(RuntimeException e,
			HttpServletRequest request){
		request.setAttribute("exception", e);		
		return "error";
	}

	
	//表单域的名称务必和user对象的属性名保持一致
	public String doRegister(){
		
		return "register";
	}
	
}
